/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package demo3791;

 
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import eu.mpower.mpoweractivityservice.soap.GetActivitiesForUserByDateWithIndividualRequest;

/**
* http://www.badkernel.com/2010/10/calendar-source-code-part-1/
* @author mike
*/
public class CalendarDialog extends javax.swing.JPanel
{
    private int[] daysInMonth = new int[]{31,28,31,30,31,30,31,31,30,31,30,31};
    private GregorianCalendar calendar = new GregorianCalendar();
    private Object[] days = new Object[50];
    testFrame backend;
    /** Creates new form CalendarDialog */
    public CalendarDialog(testFrame frame)
    {
        backend = frame;
        initComponents();
        createCalendarData();
    }
    public void createCalendarData()
    {
        calendar.set(Calendar.YEAR, Integer.parseInt((String)years.getSelectedItem()));
        calendar.set(Calendar.MONTH, months.getSelectedIndex());
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        int offset = calendar.get(Calendar.DAY_OF_WEEK);
        int totDays = daysInMonth[calendar.get(Calendar.MONTH)];
        if(calendar.isLeapYear(calendar.get(Calendar.YEAR)) && calendar.get(Calendar.MONTH)==1)
            totDays += 1;
        if(offset == 1)
            offset +=5;
        else
            offset -= 2;
        for(int i=0;i < totDays+offset;i++)
        {
            if(i+1-offset < 1)  calTable.setValueAt("", i/7, (i+7)%7);
            else {
                calTable.setValueAt(i+1-offset, i/7, (i+7)%7);
                GetActivitiesForUserByDateWithIndividualRequest req = new GetActivitiesForUserByDateWithIndividualRequest();
            }
        }
        for(int i=totDays+offset;i<42;i++)
        calTable.setValueAt("", i/7, (i+7)%7);
    }
    
public Date getSelectedDate()
{
int row = calTable.getSelectedRow();
int col = calTable.getSelectedColumn();
if(row != -1 && col != -1 && !(calTable.getValueAt(row, col) instanceof String))
{
GregorianCalendar cal = new GregorianCalendar();
cal.set(Calendar.YEAR, Integer.parseInt((String)years.getSelectedItem()));
cal.set(Calendar.MONTH, months.getSelectedIndex());
cal.set(Calendar.DAY_OF_MONTH, (Integer)calTable.getValueAt(row, col));
return cal.getTime();
}
else
return null;
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
//
private void initComponents() {
months = new javax.swing.JComboBox();
years = new javax.swing.JComboBox();
jScrollPane1 = new javax.swing.JScrollPane();
calTable = new javax.swing.JTable();
jLabel1 = new javax.swing.JLabel();
selDate = new javax.swing.JTextField();
//java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("calendarwidget/Bundle"); // NOI18N
setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(""), "Calendar")); // NOI18N
months.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }));
months.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
monthsItemStateChanged(evt);
}
});
years.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020" }));
years.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
yearsItemStateChanged(evt);
}
});
calTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null}
},
new String [] {
"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
}
));
calTable.setRowSelectionAllowed(false);
calTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
calTable.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
calTableMouseClicked(evt);
}
});
jScrollPane1.setViewportView(calTable);
calTable.getColumnModel().getColumn(0).setHeaderValue("man"); // NOI18N
calTable.getColumnModel().getColumn(1).setHeaderValue("tir"); // NOI18N
calTable.getColumnModel().getColumn(2).setHeaderValue("ons"); // NOI18N
calTable.getColumnModel().getColumn(3).setHeaderValue("tor"); // NOI18N
calTable.getColumnModel().getColumn(4).setHeaderValue("fre"); // NOI18N
calTable.getColumnModel().getColumn(5).setHeaderValue("lør"); // NOI18N
calTable.getColumnModel().getColumn(6).setHeaderValue("søn"); // NOI18N
jLabel1.setText("CalendarDialog.jLabel1.text"); // NOI18N
selDate.setText("CalendarDialog.selDate.text"); // NOI18N
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(months, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(years, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 348, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(selDate, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(months, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(years, javax.swing.GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(selDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(17, 17, 17))
);
}//
private void yearsItemStateChanged(java.awt.event.ItemEvent evt) {
createCalendarData();
}
private void monthsItemStateChanged(java.awt.event.ItemEvent evt) {
createCalendarData();
}
private void calTableMouseClicked(java.awt.event.MouseEvent evt) {
if(getSelectedDate()!=null)
selDate.setText(getSelectedDate().toString());
else
selDate.setText("No date selected");
}
// Variables declaration - do not modify
private javax.swing.JTable calTable;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JComboBox months;
private javax.swing.JTextField selDate;
private javax.swing.JComboBox years;
// End of variables declaration

}
